import Vue from 'vue'
import VueRouter from 'vue-router'
import {
  Message
} from 'element-ui'
Vue.use(VueRouter)

//避免路由重复跳转
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [{
    path: '/',
    name: '主页',
    hidden: true,
    component: () => {
      return import('../views/Index.vue')
    }
  },
  {
    path: '/phoneIndex/:id',
    name: '手机套餐',
    hidden: true,
    component: () => {
      return import('../views/phone/PhoneIndex')
    }
  },
  {
    path: '/broadBandIndex/:id',
    name: '宽带套餐',
    hidden: true,
    component: () => {
      return import('../views/broadband/BroadBandIndex')
    }
  },
  {
    path: '/trafficIndex/:id',
    name: '流量套餐',
    hidden: true,
    component: () => {
      return import('../views/traffic/TrafficIndex')
    }
  },
  {
    meta: {
      requireAuth: true,
    },
    path: '/myOrder/:username',
    name: '我的订单',
    hidden: true,
    component: () => {
      return import('../views/MyOrder')
    }
  },
  {
    meta: {
      requireAuth: true,
    },
    path: '/myInfo/:username',
    name: '个人信息',
    hidden: true,
    component: () => {
      return import('../views/MyInfo')
    }
  },
  {
    meta: {
      // requireAuth: true,
      // requireToken: true
    },
    path: '/resetPassword',
    name: '重置密码',
    hidden: true,
    component: () => {
      return import('../views/ResetPassword.vue')
    }
  },
  {
    path: '/admin',
    name: '管理员登录',
    hidden: true,
    component: () => {
      return import('../views/admin/AdminLogin')
    }
  },
  {
    meta: {
      requireAuth: true,
      role: '管理员'
    },
    path: '/adminIndex',
    name: '管理员',
    icon: 'el-icon-s-custom',
    component: () => {
      return import('../views/admin/AdminIndex');
    },
    children: [{
        meta: {
          requireAuth: true,
          role: '管理员'
        },
        path: '/adminCustomer',
        name: '顾客管理',
        icon: 'el-icon-s-management',
        component: () => import('../views/admin/AdminCustomer.vue')
      },
      {
        meta: {
          requireAuth: true,
          role: '管理员'
        },
        path: '/adminOrder',
        name: '订单管理',
        icon: 'el-icon-s-flag',
        component: () => import('../views/admin/AdminOrder.vue')
      },
    ]
  },
  {
    meta: {
      requireAuth: true,
      role: '管理员'
    },
    path: '/adminData',
    name: '数据报表',
    icon: 'el-icon-data-line',
    component: () => import('../views/admin/AdminIndex.vue'),
    children: [{
        meta: {
          requireAuth: true,
          role: '管理员'
        },
        path: '/adminCustomerData',
        name: '客户订购',
        icon: 'el-icon-sell',
        component: () => import('../views/admin/AdminCustomerData.vue')
      },
      {
        meta: {
          requireAuth: true,
          role: '管理员'
        },
        path: '/adminSupplierData',
        name: '供应商结算',
        icon: 'el-icon-suitcase-1',
        component: () => import('../views/admin/AdminSupplierData.vue')
      },
    ]
  },
  {
    meta: {
      requireAuth: true,
      role: '价格审批员'
    },
    path: '/adminCheck',
    name: '审批管理',
    icon: 'el-icon-data-line',
    component: () => import('../views/admin/AdminIndex.vue'),
    children: [{
      meta: {
        requireAuth: true,
        role: '价格审批员'
      },
      path: '/adminCheck',
      name: '价格审批',
      icon: 'el-icon-sell',
      component: () => import('../views/admin/AdminCheck.vue')
    }]
  },
  {
    meta: {
      requireAuth: true,
      role: '风险审批员'
    },
    path: '/adminCheckIndex',
    name: '审批管理',
    icon: 'el-icon-data-line',
    component: () => import('../views/admin/AdminIndex.vue'),
    children: [{
      meta: {
        requireAuth: true,
        role: '风险审批员'
      },
      path: '/adminCheck',
      name: '风险审批',
      icon: 'el-icon-sell',
      component: () => import('../views/admin/AdminCheck.vue')
    }]
  },
  {
    meta: {
      requireAuth: true,
      role: '领导'
    },
    path: '/adminCheckIndex',
    name: '审批管理',
    icon: 'el-icon-data-line',
    component: () => import('../views/admin/AdminIndex.vue'),
    children: [{
      meta: {
        requireAuth: true,
        role: '领导'
      },
      path: '/adminCheck',
      name: '领导审批',
      icon: 'el-icon-sell',
      component: () => import('../views/admin/AdminCheck.vue')
    }]
  },
]

const router = new VueRouter({
  routes
})

export default router

// 添加全局路由拦截
router.beforeEach((to, from, next) => {

  let role = sessionStorage.getItem("role");
  console.log('我的角色是：' + role);

  if (to.meta.requireAuth) {
    let targetRole = to.meta.role;
    console.log(`目标角色是${targetRole}`)
    //如果去的页面需要认证 那么就开始认证是否登录成功(通过session当中是否有存入的用户信息)
    if (sessionStorage.getItem("userInfo") !== null) {
      //如果登录成功 判断去的路由是否需要身份验证
      if (role === targetRole) {
        next();
      } else {
        if (role !== "普通用户") {
          next();
        }
      }
    } else {
      //否则下一步也放行 但放行到登录页面
      next({
        path: '/'
      })
      Message.error('您好，请您登录！');
    }
  } else {
    //如果不需要授权 比如直接去登录页面
    next()
  }
})